#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 10;
typedef long long LL;
LL a[N];
LL n;
int main()
{
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	sort(a + 1, a + n + 1);
	LL target = a[n / 2];
	LL ret = 0;
	for (int i = 1; i <= n; i++) ret += abs(target - a[i]);
	cout << ret << endl;
	return 0;
}